var ROUND_TABLE = "roundTable";
var tableId = 0;

$.extend(true, RoundTable.prototype, objectBase);


// Class for creating custom rectangular shapes like bar
function RoundTable(options) {

    // Default Options
    var defaultOptions = {
        top: 0,
        left: 0,
        size: 44,
        id: "t" + tableId++,
        orientation: 0,
        color: "black",
        background: "white",
        border: "1px solid black",
        seatCount: 8,
        seatOffset: 36,
        seatBackground: "white",
        seatSize: 14,
        seatBorder: "1px solid black",
        seatRadius: 4,
        fontSize: 16,
        reservable: true
    };

    // Create current configuration
    this.state = $.extend(true, defaultOptions, options);
}

// Method for creating HTML element
RoundTable.prototype.getElement = function () {
    if (this.element){
        return this.element
    }
    var element;

    element = $("<div/>");

    this.updateElement(element);

    return element;
};

// Method providing access to move with the object
RoundTable.prototype.resize = function (width, height){
    this.getElement().width(width);
    this.getElement().height(height);
};

RoundTable.prototype.getWidth = function (){
    return this.state.size;
};

RoundTable.prototype.getHeight = function (){
    return this.state.size;
};

RoundTable.prototype.setState = function (state){
    this.state = state;
    this.updateElement(this.element);
};

RoundTable.prototype.updateElement = function (element){
    element.empty();
    element.width(this.state.size);
    element.height(this.state.size);

    element.attr("data-type", ROUND_TABLE);
    element.attr("id", this.state.id);

    element.css("position", "absolute");
    element.css("left", this.state.left + "px");
    element.css("top", this.state.top + "px");
    element.css("background", this.state.background);

    element.css("font-size", this.state.fontSize + "px");

    element.css("line-height", this.state.size + "px");
    element.css("text-align", "center");
    element.text(this.state.id);
    element.css("color", this.state.color);

    if (this.state.border){
        element.css("border", this.state.border);
        var radius = this.state.size / 2;
        element.css("border-radius", radius + "px");
        element.css("moz-border-radius", radius + "px");
    }

    if (this.state.orientation){
        element.css("transform", "rotate(" + this.state.orientation + "deg)");
        element.css("-o-transform", "rotate(" + this.state.orientation + "deg)");
        element.css("-ms-transform", "rotate(" + this.state.orientation + "deg)");
        element.css("-moz-transform", "rotate(" + this.state.orientation + "deg)");
        element.css("-webkit-transform", "rotate(" + this.state.orientation + "deg)");
    }

    if (this.state.reservable){
        element.addClass("reservable");
    } else {
        element.removeClass("reservable");
    }

    var count = this.state.seatCount;
    var angleStripDeg = 360 / count;
    var angleStripRad = (Math.PI * 2) / count;
    for(var i = 0; i < count; i++){
        var x = - Math.sin(angleStripRad * i) * this.state.seatOffset + (this.state.size / 2) - (this.state.seatSize / 2) - 1;
        var y = Math.cos(angleStripRad * i) * this.state.seatOffset + (this.state.size / 2) - (this.state.seatSize / 2) - 1;
        var seat = new Seat({
            id: this.state.id + "_s" + i,
            top: y,
            left: x,
            orientation: angleStripDeg * i,
            background: this.state.seatBackground,
            border: this.state.seatBorder,
            size: this.state.seatSize,
            radius: this.state.seatRadius,
            reservable: this.state.reservable
        });
        seat.appendTo(element);
    }


    this.element = element;
};
